module.exports = {
  /**
   * 查询方法
   * @param {String} model 模型字符串
   * @param {Object} options 查询参数
   * @param {String} method 查询方法
   * @param {Boolean} selectDelete 是否查询已删除的
  */
  async find(model, options, method = 'findOne', selectDelete = false) {
    const reqModel = this.handleModel(model)
    if (!options.where) {
      options.where = {}
    }
    if (!selectDelete) {
      options.where.deleteFlag = false
    }
    const res = await reqModel[method](options)
    return res
  },
  /**
   * 分页查询
   * @param {String} model 模型字符串
   * @param {Object} query url query
   * @param {Object} conditions 查询参数
   * @param {Array} customOrder 自定义排序
  */
  async pageFind(model, query, conditions = {}, customOrder = []) {
    const reqModel = this.handleModel(model)
    const limit = query.pageSize ? +query.pageSize : 10 // 每页条数
    const offset = query.pageNo ? (+query.pageNo - 1) * limit : 0 // 跳过几条查询
    const _order = query._order ? query._order : 'DESC' // 排序顺序
    const _sort = query._sort ? query._sort : 'createTime' // 排序字段
    const options = {
      ...conditions,
      limit,
      offset,
      order: [...customOrder, [_sort, _order]]
    }
    if (!options.where) {
      options.where = {}
    }
    options.where.deleteFlag = false // 排除被删除的
    const data = await reqModel.findAndCountAll(options)
    const res = {
      records: data.rows.map(item => {
        return item.dataValues
      }),
      volumePage: limit,
      totalCount: data.count,
      startIndex: offset,
      totalPage: Math.ceil(data.count / limit)
    }
    return res
  },
  /**
   * 新增方法
   * @param {String} model 模型字符串
   * @param {Object} data 新增数据
  */
  async create(model, data, options) {
    const reqModel = this.handleModel(model)
    const createUser = this.state.userInfo ? this.state.userInfo.id : ''
    const lastUser = this.state.userInfo ? this.state.userInfo.id : ''
    const res = await reqModel.create({
      ...data,
      createUser,
      lastUser
    }, options)
    return res
  },
  /**
   * 批量新增方法
   * @param {String} model 模型字符串
   * @param {Array} data 新增数据
  */
  async bulkCreate(model, data, options) {
    const reqModel = this.handleModel(model)
    const realData = data.map(item => {
      return {
        ...item,
        createUser: this.state.userInfo.id,
        lastUser: this.state.userInfo.id
      }
    })
    const res = await reqModel.bulkCreate(realData, options)
    return res
  },
  /**
   * 删除
   * @param {String} model 模型字符串
   * @param {Object} data 删除查询参数
  */
  async delete(model, options) {
    const reqModel = this.handleModel(model)
    const res = await reqModel.update({ deleteFlag: true, lastUser: this.state.userInfo.id }, options)
    return res
  },
  /**
   * 修改方法
   * @param {String} model 模型字符串
   * @param {Object} data 修改数据
   * @param {Object} options 修改查询参数
  */
  async update(model, data, options) {
    const reqModel = this.handleModel(model)
    const res = await reqModel.update({
      ...data,
      lastUser: this.state.userInfo.id
    }, options)
    return res
  },
  /**
   * 根据条件计数
   * @param {String} model 模型字符串
   * @param {Object} options 查询参数
   */
  async count(model, options = {}) {
    const reqModel = this.handleModel(model)
    const count = await reqModel.count(options)
    return count
  },
  /**
   * 计算某一行的总数
   * @param {String} model 模型字符串
   * @param {Object} options 查询参数
   */
  async sum(model, row, options = {}) {
    const reqModel = this.handleModel(model)
    const count = await reqModel.sum(row, options)
    return count
  },
  /**
   * 模型处理
   * @param {String} model 模型字符串
  */
  handleModel(model) {
    let reqModel = this.model
    model = model.split('.')
    for (let index = 0; index < model.length; index++) {
      reqModel = reqModel[model[index]]
    }
    return reqModel
  }
}
